package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.article.domain.*;
import cn.wolfcode.luowowo.article.service.*;
import cn.wolfcode.luowowo.common.query.DestinationQueryObject;
import cn.wolfcode.luowowo.common.query.TravelQueryObject;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/destination")
public class DestinationController {

    @Reference
    private IRegionService regionService;

    @Reference
    private IDestinationService destinationService;

    @Reference
    private IStrategyCatalogService strategyCatalogService;

    @Reference
    private IStrategyDetailService strategyDetailService;

    @Reference
    private ITravelService travelService;

    /**
     * 前端页面热门目的地的区域查询
     *
     * @return
     */
    @RequestMapping("")
    public String list(@ModelAttribute("qo") DestinationQueryObject qo, Model model) {
        List<Region> list = regionService.selectHotRegion();
        model.addAttribute("hotRegions", list);
        return "destination/index";
    }

    /**
     * 前端页面热门目的地区域下的目的地的查询
     * 根据区域id查询所有目的地(关联地区的查询)
     * <p>
     * 资源：  /region/listDestByRegionId
     * 参数：  rid=xxx
     * 返回：  [    {      name: xxx    },    { ... },    ...  ]
     *
     * @param regionId
     * @return
     */
    @RequestMapping("/listHotDestByRegionId")
    public Object listHotDestByRegionId(@ModelAttribute("regionId") Long regionId, Model model) {
        //发送额外sql 查询省下面的市
        List<Destination> dest = destinationService.listHotDestByRegionId(regionId);
        //左右分割开
        List<Destination> leftDests = dest.subList(0, dest.size() / 2);//左边
        List<Destination> rightDests = dest.subList(dest.size() / 2, dest.size());
        model.addAttribute("leftDests", leftDests);
        model.addAttribute("rightDests", rightDests);
        return "destination/hotdestTpl";
    }

    /**
     * 游玩指南,土司界面
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/guide")
    public Object guide(Long id, Model model) {
        List<Destination> toasts = destinationService.listToast(id);
        //toasts吐司
        model.addAttribute("toasts", toasts);

        //dest目的地
        model.addAttribute("dest", toasts.remove(toasts.size() - 1));

        //catalogs该目的地下面的分类
        model.addAttribute("catalogs", strategyCatalogService.listByDestId(id));

        //strategyDetails点击量前3的文章
        model.addAttribute("strategyDetails", strategyDetailService.listViewnumTop3(id));

        return "destination/guide";
    }

    @RequestMapping("/surveyPage")
    public Object surveyPage(@ModelAttribute("qo") DestinationQueryObject qo, Model model) {
        List<Destination> toasts = destinationService.listToast(qo.getDestId());
        //土司
        model.addAttribute("toasts", toasts);
        //目的地
        model.addAttribute("dest", toasts.remove(toasts.size() - 1));
        return "destination/survey";
    }

    /**
     * 该目的地下面的所有分类,当前选中的分类,该分类下面的第一篇文章的内容
     * 带tpl的均为嵌入式网页
     *
     * @param qo
     * @param model
     * @return
     */
    @RequestMapping("/survey")
    public Object survey(@ModelAttribute("qo") DestinationQueryObject qo, Model model) {
        //查询目的地下面的所有分类
        List<StrategyCatalog> catalogs = strategyCatalogService.listByDestId(qo.getDestId());
        //catalogs 该目的地下面的所有分类
        model.addAttribute("catalogs", catalogs);
        //循环遍历分类,当分类的id等于当前选中的id
        for (StrategyCatalog catalog : catalogs) {
            if (catalog.getId().equals(qo.getCatalogId())) {
                //catalog 当前选中的分类
                model.addAttribute("catalog", catalog);
                //从分类中取出第一篇文章,并查询该文章的内容
                StrategyDetail detail = catalog.getDetails().get(0);
                StrategyContent content = strategyDetailService.getContent(detail.getId());
                detail.setStrategyContent(content);
                //detail 该选中分类下面的第一篇文章的内容
                model.addAttribute("detail", detail);
                //结束当前的循环
                break;
            }
        }
        return "destination/surveyTpl";
    }

    /**
     * 资源：
     * /destination/travels
     * 参数：
     * currentPage=xxx
     * destIde=xxx
     * orderTypee=xxx
     * travelTimeTypee=xxx
     * perExpendTypee=xxx
     * dayTypee=xxx
     * 返回：
     * travelTpl.ftl页面
     *
     * @param qo
     * @param model
     * @return
     */
    @RequestMapping("/travels")
    public Object travels(TravelQueryObject qo, Model model) {

        model.addAttribute("pageInfo", travelService.query(qo));
        return "destination/travelTpl";
    }


}
